<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>Data-Table 表格</title>
    <link rel="stylesheet" href="/res/frame/layui/css/layui.css">
    <link rel="stylesheet" href="/res/frame/static/css/style.css">
    <link rel="icon" href="/res/frame/static/image/code.png">
</head>
<body class="body">

<!-- 工具集 -->
<div class="my-btn-box">
    <span class="fl">
        <a class="layui-btn btn-add btn-default" id="btn-refresh">刷新
            <i class="layui-icon">&#x1002;</i></a>
         <a class="layui-btn btn-add btn-default" id="btn-add" onclick="editChild('0')">添加</a>
    </span>
    <span class="fl">
          <span class="layui-form-label">姓名：</span>
        <div class="layui-input-inline" style="float: left;">
            <input type="text" id="name" autocomplete="off" placeholder="姓名" class="layui-input">
        </div>
        <span class="layui-form-label">电话号码：</span>
        <div class="layui-input-inline" style="float: left;">
            <input type="text" id='phone' autocomplete="off" placeholder="电话"
                   class="layui-input">
        </div>
         <button class="layui-btn" data-type="reload">搜索</button>
    </span>
</div>

<!-- 表格 -->
<table class="layui-hide" id="dateTable" lay-filter="userList"></table>

<script type="text/javascript" src="/res/frame/layui/layui.js"></script>
<script type="text/javascript" src="/res/js/index.js"></script>
<script type="text/javascript">
    // layui方法
    layui.use(['table', 'form', 'layer', 'vip_table', 'cookie', 'laydate'], function () {

        // 操作对象
        var form = layui.form
            , table = layui.table
            , layer = layui.layer
            , vipTable = layui.vip_table
            , $ = layui.jquery,
            cookie = layui.cookie,
            laydate = layui.laydate;

        // 表格渲染
        var tableIns = table.render({
            elem: '#dateTable'                  //指定原始表格元素选择器（推荐id选择器）
            , height: vipTable.getFullHeight()
            , defaultToolbar: ['exports', 'exports']
            , cols: [[
                {type: 'numbers', fixed: 'left', title: '序号'}
                , {field: 'clChName', title: '姓名', width: 180}
                , {field: 'pkGuid', hide: true}
                , {field: 'clChPhone', title: '电话号码', width: 120}
                , {
                    field: 'clChStatus', title: '状态', width: 120, templet: function (d) {
                        return d.clChStatus == "UL" ? "有效" : "锁定";
                    }
                }
                , {field: 'curNum', title: '今日认领数', width: 120}
                , {field: 'clChLogindate', title: '登陆时间', width: 180, align: 'center'}
                , {field: 'clChCreatedate', title: '注册时间', width: 180, align: 'center'}
                , {
                    fixed: 'right', title: '操作', width: 200, align: 'center', templet: function (d) {
                        return d.clChStatus == "UL" ? "<a class=\"layui-btn layui-btn-sm layui-btn-radius layui-btn-normal\" lay-event=\"edit\">修改</a>\n" +
                            "    <a class=\"layui-btn layui-btn-sm layui-btn-radius layui-btn-danger\" lay-event=\"lock\">锁定</a>" :
                            "<a class=\"layui-btn layui-btn-sm layui-btn-radius layui-btn-normal\" lay-event=\"edit\">修改</a>\n" +
                            "    <a class=\"layui-btn layui-btn-sm layui-btn-radius layui-btn-danger\" lay-event=\"unlock\">解锁</a>"
                    }
                }
            ]]
            , id: 'tableReload'
            , url: '/chilednode/listByPage'
            , where: {
                clId: $.cookie("clientId"),
            }
            , method: 'post'
            , page: true
            , contentType: 'application/json'
            , request: {
                pageName: 'pageNum' //页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list //解析数据列表
                }
            }, response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                , statusCode: "200" //规定成功的状态码，默认：0
                , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                , countName: 'count' //规定数据总数的字段名称，默认：count
                , dataName: 'data' //规定数据列表的字段名称，默认：data
            }
            , loading: false
            , limits: [30, 60, 90, 150, 300]
            , limit: 30 //默认采用30
            , done: function (res, curr, count) {
            }
        });
        //搜索数据
        var active = {
            reload: function () {
                //执行重载
                table.reload('tableReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        clId: $.cookie("clientId"),
                        name: $("#childName").val(),
                        phone: $("#phone").val()
                    }
                });
            },
            getCheckData: function () { //获取选中数据
                var checkStatus = table.checkStatus('tableReload');
                var data = checkStatus.data;
                console.log(data);
                var cusIds = [];
                $.each(data, function (i, val) {
                    cusIds.push(val.pkGuid);

                });
                var params = {
                    "clId": $.cookie("clientId"),
                    "cusIds": cusIds
                }
                console.log(cusIds);
                console.log(params);
                var s = JSON.stringify(params);
                $.ajax({
                    url: "/client/batchBuy",
                    type: "post",
                    dataType: "json",
                    data: s,
                    contentType: "application/json",
                    success: function (result) {
                        console.log(result)
                        if (result.code === 200) {
                            alert(result.msg)
                            tableIns.reload();
                        } else {
                            alert("操作失败");
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.log(XMLHttpRequest.responseText);
                    }
                });


            }
        };

        $('.my-btn-box .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        // 刷新
        $('#btn-refresh').on('click', function () {
            tableIns.reload();
        });
        table.on('tool(userList)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'lock') {
                layer.confirm('确定锁定用户', function (index) {
                    params = {};
                    params.pkGuid = data.pkGuid;
                    params.clChStatus = 'L';
                    $.ajax({
                        url: "/chilednode/update",
                        type: "post",
                        data: JSON.stringify(params),
                        contentType: "application/json",
                        success: function (result) {
                            console.log(result)
                            if (result.code === 200) {
                                layer.alert(result.msg)
                                tableIns.reload();
                            } else {
                                layer.alert(result.msg)
                                tableIns.reload();
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            console.log(XMLHttpRequest.responseText);
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                    editChild(data.pkGuid);
            } else if (layEvent === 'unlock') {
                layer.confirm('确定解锁用户', function (index) {
                    params = {};
                    params.pkGuid = data.pkGuid;
                    params.clChStatus = 'UL';
                    $.ajax({
                        url: "/chilednode/update",
                        type: "post",
                        data: JSON.stringify(params),
                        contentType: "application/json",
                        success: function (result) {
                            console.log(result)
                            if (result.code === 200) {
                                layer.alert(result.msg)
                                tableIns.reload();
                            } else {
                                layer.alert(result.msg)
                                tableIns.reload();
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            console.log(XMLHttpRequest.responseText);
                        }
                    });
                });

            }
        });
    });
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }


    function editChild(pkGuid) {
        layer.open({
            title: '添加子账号',
            type: 2,
            anim: 5,
            area: ['500px', '400px'],
            content: ['/res/html/client/editChildInfo.html?pkGuid=' + pkGuid, 'no'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
        });

    }


</script>
<!-- 表格操作按钮集 -->
<script type="text/html" id="barOption1">
    <a class="layui-btn layui-btn-sm layui-btn-radius layui-btn-normal" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger" lay-event="lock">锁定</a>
</script>
<script type="text/html" id="barOption2">
    <a class="layui-btn layui-btn-sm layui-btn-radius layui-btn-normal" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger" lay-event="unlock">解锁</a>
</script>


</body>
</html>